package com.example.dbmysql.Controller;


import com.example.dbmysql.pojo.User;
import com.example.dbmysql.result.Result;
import com.example.dbmysql.service.UserService;
import com.example.dbmysql.utils.JwtUtil;
import com.example.dbmysql.utils.Md5Util;
import com.example.dbmysql.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/user")
@Validated
public class userController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(@Pattern(regexp = "^\\S{4,16}$") String username, @Pattern(regexp = "^\\S{4,16}$") String password) {
        System.out.println(username);
        System.out.println(password);
        // 先查询用户
        User u = userService.findByUserName(username);
        // 没有占用
        if (u == null) {
            userService.register(username, password);
            return Result.success();
        } else {
            // 占用
            return Result.error("用户名已注册");
        }
    }

    @PostMapping("/login")
    public Result login(@Pattern(regexp = "^\\S{4,16}$") String username, @Pattern(regexp = "^\\S{4,16}$") String password) {
        // 更具用户名查询用户
        System.out.println(username+"<====>"+password);
        User LoginUser = userService.findByUserName(username);
        if (LoginUser == null) {
            return Result.error("用户还未注册");
        }

        if (Md5Util.getMD5String(password).equals(LoginUser.getPassword())) {
            // 登录成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", LoginUser.getId());
            claims.put("username", LoginUser.getUsername());
            String token = JwtUtil.genToken(claims);
            return Result.success(token);
        } else {
            return Result.error("验证失败");
        }
    }

    /*
    *
    * 获取用户信息 权限 (1 2 3 4 )
    *
    *   1 admin 权限
    *   2 项目经理权限
    *   3 开发组组长权限
    *   4 普通用户权限
    *
    * */

    @GetMapping("/userInfo")
    public Result<User> userInfo() {

        Map<String, Object> map = ThreadLocalUtil.get();

        String username = (String) map.get("username");

        User user = userService.findByUserName(username);


        switch (user.getRole()){
            case 1:

                System.out.println("最高权限");
                break;

            case 2:

                System.out.println("权限2");
                break;

            case 3:

                System.out.println("权限3");
                break;

            case 4:

                System.out.println("权限4");
                break;
        }



        return Result.success(user);
    }

    @PutMapping("/update")
    public Result update(@RequestBody @Validated User user) {

        userService.update(user);

        return Result.success();
    }

    @DeleteMapping("/delUser")
    public Result delUser(@RequestBody User user) {


        User u = userService.findByUserName(user.getUsername());

        if (u == null) {
            return Result.error("用户不存在");
        } else {
            if (!Objects.equals(u.getId(), user.getId())) return Result.error("用户id与用户名不匹配");
            userService.delUserById(user);
            return Result.success();
        }

    }
}
